﻿//-----------------------------------------------------------------------
// <copyright file="Singleton.cs" company="CodePlex">
//     Copyright (c) Doug Holland, CodePlex. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace CodePlex.DesignPatterns
{
    using System;
    using System.Threading;

    /// <summary>
    /// Provides a template for the Singleton design pattern implemented within the C# programming language.
    /// </summary>
    /// <remarks>
    /// <para>
    /// Given that the constructor for the <see cref="T:Singleton"/> class is private, an instance of the class can
    /// only be created from within the class declaration itself. Therefore the <see cref="T:Singleton"/> class 
    /// contains a static instance field of type <see cref="T:Singleton"/> and a static read only property that first
    /// verifies if the static instance field is <c>null</c> and only creates an instance of the <see cref="T:Singleton"/>
    /// class if one does not already exist. Thread safety is ensured through the use of the double-check locking pattern.
    /// </para>
    /// <para>
    /// It should be noted however that the <see cref="T:Singleton"/> design pattern, when implemented within the .net
    /// platform, only ensures that the instance is unique within the given application domain. If an instance needs to
    /// be unique across multiple application domains then it will be necessary to write some unmanaged code to enforce
    /// that restriction.
    /// </para>
    /// </remarks>
    public sealed class Singleton
    {
        #region Singleton Private Fields

        /// <summary>
        /// Unique instance of the <see cref="T:Singleton"/> class.
        /// </summary>
        private static Singleton instance;

        /// <summary>
        /// Private static thread synchronization object.
        /// </summary>
        private static object internalSyncObject;

        #endregion

        #region private Singleton()

        /// <summary>
        /// Prevents a default instance of the <see cref="T:Singleton"/> class from being created.
        /// </summary>
        private Singleton()
        {
        }

        #endregion

        #region private static object InternalSyncObject

        /// <summary>
        /// Gets the private thread synchronization object.
        /// </summary>
        /// <value>The private thread synchronization object.</value>
        private static object InternalSyncObject
        {
            get
            {
                if (Singleton.internalSyncObject == null)
                {
                    object obj = new object();

                    Interlocked.CompareExchange(ref Singleton.internalSyncObject, obj, null);
                }

                return Singleton.internalSyncObject;
            }
        }

        #endregion
        
        #region public static Singleton UniqueInstance()

        /// <summary>
        /// Gets the unique instance of the <see cref="T:Singleton"/> class.
        /// </summary>
        /// <returns>The unique instance of the <see cref="T:Singleton"/> class.</returns>
        public static Singleton UniqueInstance
        {
            get
            {
                lock (Singleton.InternalSyncObject)
                {
                    if (instance == null)
                    {
                        // use the double-check locking pattern.
                        if (Singleton.instance == null)
                        {
                            Singleton.instance = new Singleton();
                        }
                    }

                    return instance;
                }
            }
        }

        #endregion
    }
}
